

//
//  LGApplyRefundCredentialsCell.m
//  LuxuryGoods
//
//  Created by DCQ on 2018/7/24.
//  Copyright © 2018年 Future. All rights reserved.
//


#import "LGApplyRefundCredentialsCell.h"
#import "CompleteGoodsImgVCell.h"
#import "CompleteGoodsAddImgVCell.h"
static NSString *const CompleteGoodsImgVCellIdentifier = @"CompleteGoodsImgVCellIdentifier";

static NSString *const CompleteGoodsAddImgVCellIdentifier = @"CompleteGoodsAddImgVCellIdentifier";
@interface LGApplyRefundCredentialsCell()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(nonatomic, strong) NSMutableArray *selectedPhotos;//图片数据
@property(nonatomic, strong) NSMutableArray *selectedAssets;
@end
@implementation LGApplyRefundCredentialsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.collectionView.delegate = self;
    self.collectionView.dataSource= self;
    [self registerCell];
}
- (void)registerCell{
    
    [self.collectionView registerClass: [CompleteGoodsImgVCell class]  forCellWithReuseIdentifier:CompleteGoodsImgVCellIdentifier];
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CompleteGoodsAddImgVCell class]) bundle:nil] forCellWithReuseIdentifier:CompleteGoodsAddImgVCellIdentifier];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setupselectedPhotos:(NSMutableArray *)selectedPhotos  withSelectedAssets:(NSMutableArray *)selectedAssets{
    
    self.selectedPhotos = selectedPhotos;
    self.selectedAssets = selectedAssets;
    [self.collectionView reloadData];
}
#pragma mark --

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger row = 1;
   
        if (self.selectedPhotos.count ) {
            if ( [self.selectedPhotos count] < 3) {
                row = self.selectedPhotos.count + 1;
            }else{
                row = self.selectedPhotos.count;
            }
        }
   
    return row;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
     UICollectionViewCell *cell = nil;
    if (indexPath.row == self.selectedPhotos.count) {
        cell = [self getAddImgVCollectionView:collectionView withIndexPath:indexPath];
    }else{
        cell = [self getImgVCollectionView:collectionView withIndexPath:indexPath];
    }
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
- (UICollectionViewCell *)getImgVCollectionView:(UICollectionView *)collectionView withIndexPath:(NSIndexPath *)indexPath{
    
    CompleteGoodsImgVCell * tempCell = [collectionView dequeueReusableCellWithReuseIdentifier:CompleteGoodsImgVCellIdentifier forIndexPath:indexPath];
    tempCell.imageView.image = self.selectedPhotos[indexPath.row];
    tempCell.asset = self.selectedAssets[indexPath.row];
    tempCell.deleteBtn.hidden = NO;
    tempCell.gifLable.hidden = YES;
    tempCell.deleteBtn.tag = indexPath.row;
    [tempCell.deleteBtn addTarget:self action:@selector(deleteBtnClik:) forControlEvents:UIControlEventTouchUpInside];
    return tempCell;
}
- (void)deleteBtnClik:(UIButton *)sender {
    [_selectedPhotos removeObjectAtIndex:sender.tag];
    [_selectedAssets removeObjectAtIndex:sender.tag];
    [self.collectionView reloadData];
    
}
- (UICollectionViewCell *)getAddImgVCollectionView:(UICollectionView *)collectionView withIndexPath:(NSIndexPath *)indexPath{
    
    CompleteGoodsAddImgVCell * tempCell = [collectionView dequeueReusableCellWithReuseIdentifier:CompleteGoodsAddImgVCellIdentifier forIndexPath:indexPath];
    [tempCell setupImgMaxNum:3 withCurrenNum:[self.selectedPhotos count]];
    return tempCell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize size =  CGSizeZero;
    size = CGSizeMake(80, 80);
    return size;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
      [self pushImagePickerController];
}
- (void)pushImagePickerController {
    if (self.selectedImgBlock) {
        self.selectedImgBlock();
    }
}


@end
